# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/28 13:15
desc: 企业清单页
'''
import time
from appium.webdriver.common.appiumby import AppiumBy
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage


class CompanyListPage(CLUEAppBasePage):
    """
    搜索结果-企业清单页面
    """
    _confirm_btn_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("确认")')
    _confirm_btn_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "确认"')

    _back_btn_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "return"')

    def get_btn_webdriver(self, company_name):
        """获取指定公司的关注按钮"""
        if self.os == 'android':
            return self.find_element((AppiumBy.XPATH, f"//*[contains(@text,'pages/company/index')]//*[@text='{company_name}']/../preceding-sibling::*[2]/*/*[2]"))
        else:
            return self.find_element((AppiumBy.XPATH, f'//XCUIElementTypeStaticText[@name="{company_name}"]/preceding::*[contains(@name,"关注")]'))

    def follow_company(self, company_name):
        """
        点击搜索列表的关注按钮
        """
        follow_btn = self.get_btn_webdriver(company_name)
        assert follow_btn.get_attribute('name') == '关注', f'企业列表页面，当前企业已是关注状态，{company_name}'
        follow_btn.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))
        time.sleep(1)
        assert follow_btn.get_attribute('name') == '已关注', '搜索结果-企业列表页面关注企业后，按钮文案没有改变'

    def unfollow_company(self, company_name):
        """
        点击搜索列表的取消关注按钮
        """
        follow_btn = self.get_btn_webdriver(company_name)
        assert follow_btn.get_attribute('name') == '已关注', f'企业列表页面，当前企业不是已关注状态，{company_name}'
        follow_btn.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))
        time.sleep(1)
        assert follow_btn.get_attribute('name') == '关注', '企业列表页面关注企业后，按钮文案没有改变'

    def go_back(self):
        """IOS端driver.back()不work，需要使用后退按钮

        :return:
        """
        if self.os=='android':
            self.driver.back()
        else:
            self.find_element_and_click(getattr(self, '_back_btn_locator_' + self.os))